package com.zl.learning.swing;

import javax.swing.*;
import java.awt.*;
import java.io.File;

public class ImageView {
	public static void main(String[] args) {
		EventQueue.invokeLater(() -> {
			JFrame frame = new ImageViewFrame();
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setVisible(true);
		});
	}

	static class ImageViewFrame extends JFrame {

		private static final long serialVersionUID = 1L;
		private static final int DEFAULT_WIDTH = 800;
		private static final int DEFAULT_HEIGTHT = 600;

		private JLabel label;
		private JFileChooser chooser;
		public ImageViewFrame() throws HeadlessException {
			try {
	//			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e1) {
				e1.printStackTrace();
			}
			this.setTitle("图片浏览器");
			this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGTHT);
			//用来显示图片
			label = new JLabel();
			add(label);

			chooser = new JFileChooser();
			chooser.setCurrentDirectory(new File("."));

			JMenuBar menuBar = new JMenuBar();
			this.setJMenuBar(menuBar);

			JMenu menu = new JMenu("文件");
			menuBar.add(menu);

			JMenu menu2 = new JMenu("编辑");
			menuBar.add(menu2);

			JMenuItem openItem = new JMenuItem("打开图片");
			menu.add(openItem);
			openItem.addActionListener(e -> {
				int result = chooser.showOpenDialog(null);
				if(result==JFileChooser.APPROVE_OPTION){
					String name = chooser.getSelectedFile().getPath();
					System.out.println(name);
					label.setIcon(new ImageIcon(name));
				}
			});
			JMenuItem exitItem = new JMenuItem("退出");
			menu.add(exitItem);
			exitItem.addActionListener(e -> System.exit(0));

			JMenuItem copyItem = new JMenuItem("复制");
			menu2.add(copyItem);
		}

	}
}
